package com.study.Link;

/**
 * 双向链表模拟
 */
public class DoubleLinkStore {
    DoubleNode head;

    DoubleLinkStore (){
        head = new DoubleNode();
    }
    //遍历
    public void list(){
        DoubleNode current = this.head.next;
        while (current.next !=null){
            System.out.println(current.element);
            current = current.next;
        }
    }
    //添加
    public void addElement(DoubleNode element){
        DoubleNode current = this.head.next;
        if (current == null){
            this.head.next = element;
            element.pre = this.head;
        }
        while (true){
            if (current.next == null){
                current.next = element;
                element.pre = current.next;
                break;
            }
            current =current.next;
        }
    }
    // 删除
    public void delete(DoubleNode element){
        DoubleNode current = this.head.next;
        while (current!=null){
            if (current.element == element.element){
                current.pre.next = current.next;
                if(current.next!=null){
                    current.next.pre = current.pre;
                }
                break;
            }
            current = current.next;
        }


    }



}

class DoubleNode{

    DoubleNode pre;

    DoubleNode next;

    Object element;

}